﻿namespace KinonekoSoftware.Foundation.UI.Commands
{

    public class FuncCommand : ICommandEX
    {

        public FuncCommand(Action<object> executeHandler)
        {
            ExecuteHandler = executeHandler ?? throw new ArgumentNullException(nameof(executeHandler));
        }

        public FuncCommand(Action<object> executeHandler, Predicate<object> canExecuteHandler)
        {
            ExecuteHandler    = executeHandler    ?? throw new ArgumentNullException(nameof(executeHandler));
            CanExecuteHandler = canExecuteHandler ?? throw new ArgumentNullException(nameof(canExecuteHandler));

        }

        public bool CanExecute(object parameter) => CanExecuteHandler?.Invoke(parameter) ?? true;

        public void Execute(object parameter) => ExecuteHandler(parameter);

        public Action<object>    ExecuteHandler    { get; }
        public Predicate<object> CanExecuteHandler { get; }
        public void RaiseUpdate() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

        public event EventHandler CanExecuteChanged;
    }
}